{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Longest Alternating Subarray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #enumeration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #枚举"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: alternatingSubarray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最长交替子序列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;。如果 <code>nums</code>&nbsp;中长度为&nbsp;<code>m</code>&nbsp;的子数组&nbsp;<code>s</code>&nbsp;满足以下条件，我们称它是一个 <strong>交替子序列</strong> ：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>m</code>&nbsp;大于&nbsp;<code>1</code>&nbsp;。</li>\n",
    "\t<li><code>s<sub>1</sub> = s<sub>0</sub> + 1</code>&nbsp;。</li>\n",
    "\t<li>下标从 <strong>0</strong> 开始的子数组&nbsp;<code>s</code>&nbsp;与数组&nbsp;<code>[s<sub>0</sub>, s<sub>1</sub>, s<sub>0</sub>, s<sub>1</sub>,...,s<sub>(m-1) % 2</sub>]</code>&nbsp;一样。也就是说，<code>s<sub>1</sub> - s<sub>0</sub> = 1</code>&nbsp;，<code>s<sub>2</sub> - s<sub>1</sub> = -1</code>&nbsp;，<code>s<sub>3</sub> - s<sub>2</sub> = 1</code>&nbsp;，<code>s<sub>4</sub> - s<sub>3</sub> = -1</code>&nbsp;，以此类推，直到&nbsp;<code>s[m - 1] - s[m - 2] = (-1)<sup>m</sup></code>&nbsp;。</li>\n",
    "</ul>\n",
    "\n",
    "<p>请你返回 <code>nums</code>&nbsp;中所有 <strong>交替</strong>&nbsp;子数组中，最长的长度，如果不存在交替子数组，请你返回 <code>-1</code>&nbsp;。</p>\n",
    "\n",
    "<p>子数组是一个数组中一段连续 <strong>非空</strong>&nbsp;的元素序列。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>nums = [2,3,4,3,4]\n",
    "<b>输出：</b>4\n",
    "<b>解释：</b>交替子数组有 [3,4] ，[3,4,3] 和 [3,4,3,4] 。最长的子数组为 [3,4,3,4] ，长度为4 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>nums = [4,5,6]\n",
    "<b>输出：</b>2\n",
    "<strong>解释：</strong>[4,5] 和 [5,6] 是仅有的两个交替子数组。它们长度都为 2 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [longest-alternating-subarray](https://leetcode.cn/problems/longest-alternating-subarray/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [longest-alternating-subarray](https://leetcode.cn/problems/longest-alternating-subarray/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[2,3,4,3,4]', '[4,5,6]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans = 1\n",
    "        cur = 1\n",
    "        for i in range(1,n):\n",
    "            # 如果上个状态为奇数那么当前元素需要比上个元素大1才可以延续 反之差值须为-1\n",
    "            if nums[i] - nums[i-1] == 2*int(cur&1)-1:\n",
    "                cur += 1\n",
    "             # 以上个元素为开始的交替子数组\n",
    "            elif nums[i] - nums[i-1] == 1:\n",
    "                cur = 2\n",
    "            # 都不满足则以当前元素为起始元素\n",
    "            else:\n",
    "                cur = 1\n",
    "            ans = max(ans,cur)\n",
    "        return ans if ans>1 else -1\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        i,n = 0,len(nums)\n",
    "        while i <n-1:\n",
    "            if nums[i+1]-nums[i]!=1:\n",
    "                i+=1\n",
    "                continue\n",
    "            i0=i\n",
    "            i+=1\n",
    "            while i<n and nums[i]==nums[i0]+(i-i0)%2:\n",
    "                i+=1\n",
    "            ans = max(ans,i-i0)\n",
    "            i-=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        i = 0\n",
    "        ans =0\n",
    "        while i<n:\n",
    "            start = i\n",
    "            flag = 1\n",
    "            while start+1<n and  nums[start+1]-nums[start] == flag: \n",
    "                flag = - flag\n",
    "                start+=1\n",
    "            if start-i+1> ans:\n",
    "                ans = start-i+1\n",
    "            if start>i:\n",
    "                i = start\n",
    "            else:\n",
    "                i = start+1\n",
    "            \n",
    "        \n",
    "        return -1 if ans<=1 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        pre = 100000\n",
    "        n = len(nums)\n",
    "        i = 0\n",
    "        while i<n:\n",
    "            now = [nums[i]]\n",
    "            i += 1\n",
    "            j = i\n",
    "            while j < n:\n",
    "                if len(now)==1:\n",
    "                    if nums[j]-now[-1]==1:\n",
    "                        now.append(nums[j])\n",
    "                    else:\n",
    "                        break\n",
    "                else:\n",
    "                    if now[-1] - now[-2]==1 and nums[j] - now[-1] == -1:\n",
    "                            now.append(nums[j])\n",
    "                    elif now[-1] - now[-2]==-1 and nums[j] - now[-1] == 1:\n",
    "                            now.append(nums[j])\n",
    "                    else:\n",
    "                        break\n",
    "                j+=1\n",
    "            \n",
    "            if len(now)>1:\n",
    "                # print(now)\n",
    "                i = j-1\n",
    "                ans = max(len(now),ans)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            for j in range(i + 1, n):\n",
    "                #[i....j]\n",
    "                # 1 -1 1 -1\n",
    "                ok = True\n",
    "                t = -1\n",
    "                for k in range(i + 1, j + 1):\n",
    "                    if nums[k] - nums[k-1] != t * -1:\n",
    "                        ok = False\n",
    "                        break\n",
    "                    else:\n",
    "                        t *= -1\n",
    "                if ok:\n",
    "                    ans = max(ans, j - i + 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        arr = [1,-1]\n",
    "        t = 0\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            j = i\n",
    "            flag = True\n",
    "            while j<n-1:\n",
    "                if flag and nums[j+1]-nums[j]==1:\n",
    "                    j+=1\n",
    "                    flag = not flag\n",
    "                    ans = max(ans,j-i+1)\n",
    "                elif (not flag) and nums[j+1]-nums[j]==-1 :\n",
    "                    j+=1\n",
    "                    flag = not flag\n",
    "                    ans = max(ans,j-i+1)\n",
    "                else:\n",
    "                    break\n",
    "        return ans if ans!=0 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        i, n = 0, len(nums)\n",
    "        while i < n - 1:\n",
    "            if nums[i + 1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] == (nums[start] + (i - start) % 2):\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "            i -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        i, n = 0, len(nums)\n",
    "        while i < n - 1:\n",
    "            if nums[i + 1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            i0 = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] == nums[i0] + (i - i0) % 2:\n",
    "                i += 1\n",
    "            ans = max(ans, i - i0)\n",
    "            i -= 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        n = len(nums)\n",
    "        i =0\n",
    "        ans = -1\n",
    "        while i<n:\n",
    "            j=i+1\n",
    "            while j<n and nums[j]-nums[j-1] == (-1)**(j-i+1):\n",
    "                j+=1\n",
    "            if j-i>1:\n",
    "                ans = max(ans,j-i)\n",
    "            i +=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        group = [-1]\n",
    "        pre = 100000\n",
    "        n = len(nums)\n",
    "        i = 0\n",
    "        while i<n:\n",
    "            now = [nums[i]]\n",
    "            i += 1\n",
    "            j = i\n",
    "            while j < n:\n",
    "                if len(now)==1:\n",
    "                    if nums[j]-now[-1]==1:\n",
    "                        now.append(nums[j])\n",
    "                    else:\n",
    "                        break\n",
    "                else:\n",
    "                    if now[-1] - now[-2]==1 and nums[j] - now[-1] == -1:\n",
    "                            now.append(nums[j])\n",
    "                    elif now[-1] - now[-2]==-1 and nums[j] - now[-1] == 1:\n",
    "                            now.append(nums[j])\n",
    "                    else:\n",
    "                        break\n",
    "                j+=1\n",
    "            \n",
    "            if len(now)>1:\n",
    "                # print(now)\n",
    "                group.append(len(now))\n",
    "        # print(group)\n",
    "        return max(group)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans = -1 \n",
    "        i = 0\n",
    "        while i < n-1:\n",
    "            if nums[i+1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            a = 1\n",
    "            while i < n and nums[i] - nums[i-1] == a:\n",
    "                a = -a\n",
    "                i += 1\n",
    "            ans = max(ans,i - start)\n",
    "            i -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        res = flag = last = 1\n",
    "        for i in range(1, n, 1):\n",
    "            if nums[i] == nums[i-1] + flag:\n",
    "                last += 1\n",
    "                res = max(res, last)\n",
    "                flag = -flag\n",
    "            else:\n",
    "                last = 1\n",
    "                flag = 1\n",
    "                if nums[i] == nums[i-1] + flag:\n",
    "                    last += 1\n",
    "                    res = max(res, last)\n",
    "                    flag = -flag\n",
    "        return -1 if res == 1 else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        maxLength = -1\n",
    "        diff = 1\n",
    "        count = 1\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] - nums[i-1] == diff:\n",
    "                diff *= -1\n",
    "                count += 1\n",
    "                maxLength = max([maxLength, count])\n",
    "            elif nums[i] - nums[i-1] == 1:\n",
    "                count = 1\n",
    "                diff = 1\n",
    "                diff *= -1\n",
    "                count += 1\n",
    "                maxLength = max([maxLength, count])\n",
    "            else:\n",
    "                count = 1\n",
    "                diff = 1\n",
    "        return maxLength\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        maxx=-1\n",
    "        mid=1\n",
    "        sum=0\n",
    "        i=0\n",
    "        n=len(nums)\n",
    "        while(i<n):\n",
    "            if sum==0:\n",
    "                sum+=1\n",
    "            else:\n",
    "                if(nums[i]==(nums[i-1]+mid)):\n",
    "                    sum+=1\n",
    "                    mid=mid*(-1)\n",
    "                    maxx=max(maxx,sum)\n",
    "                else:\n",
    "                    if(sum!=1):\n",
    "                        i=i-2\n",
    "                    else:\n",
    "                        i=i-1\n",
    "                    mid=1\n",
    "                    sum=0\n",
    "            i=i+1\n",
    "        return maxx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        cnt = ans = 1\n",
    "        up = None\n",
    "        for x, y in pairwise(nums):\n",
    "            if y - x == 1:\n",
    "                if not up:\n",
    "                    cnt += 1\n",
    "                    up = True\n",
    "                else:\n",
    "                    cnt = 2\n",
    "            elif x - y == 1 and up:\n",
    "                cnt += 1\n",
    "                up = False\n",
    "            else:\n",
    "                cnt = 1\n",
    "                up = None\n",
    "\n",
    "            if cnt > ans:\n",
    "                ans = cnt\n",
    "\n",
    "        return -1 if ans == 1 else ans\n",
    "           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans,l,judge = -1,0,0\n",
    "        lenth = len(nums)\n",
    "        for i in range(1,lenth):\n",
    "            if judge == 1:\n",
    "                if (i - l) % 2 == 0 and nums[i] != nums[l]:\n",
    "                    ans = max(ans,i-l)\n",
    "                    judge = 0\n",
    "                elif (i - l) % 2 == 1 and nums[i] != nums[l+1]:\n",
    "                    ans = max(ans,i-l)\n",
    "                    judge = 0\n",
    "            if judge == 0 and nums[i] - nums[i-1] == 1:\n",
    "                l,judge = i-1,1\n",
    "        if judge == 1:\n",
    "            ans = max(ans,len(nums)-l)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        i, n = 0, len(nums)\n",
    "        while i < n - 1:\n",
    "            if nums[i + 1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            i0 = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] == nums[i0] + (i - i0) % 2:\n",
    "                i += 1\n",
    "            ans = max(ans, i - i0)\n",
    "            i -= 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        i, n = 0, len(nums)\n",
    "        res = -1\n",
    "        while i < n:\n",
    "            start = i \n",
    "            cur = 1\n",
    "            while i < n-1 and nums[i+1] - nums[i] == cur:\n",
    "                i += 1\n",
    "                cur *= -1\n",
    "            if i - start + 1 > 1: res = max(res, i - start + 1)\n",
    "            if i == n-1 or nums[i+1] - nums[i] != 1: i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        #copy\n",
    "        ans =-1\n",
    "        i,n = 0,len(nums)\n",
    "        while i <n-1:\n",
    "            if nums[i+1] -nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            i0=i\n",
    "            i+= 1\n",
    "            while i <n and nums[i]==nums[i0] + (i-i0)%2:\n",
    "                i+=1\n",
    "            ans = max(ans,i-i0)\n",
    "            i-=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        lst=[]\n",
    "        pre=len(lst)\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i]-nums[i+1]==-1:\n",
    "                lst.append(nums[i])\n",
    "                lst.append(nums[i+1])\n",
    "                for j in range(i+2,len(nums)):\n",
    "                    if nums[j]-lst[-1]==lst[-2]-lst[-1]:\n",
    "                        lst.append(nums[j])\n",
    "                    else:\n",
    "                        break\n",
    "                pre=max(len(lst), pre)\n",
    "                lst.clear()\n",
    "            else:\n",
    "                continue\n",
    "        if pre==0:\n",
    "            return -1\n",
    "        else:\n",
    "            return pre"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        N = len(nums)\n",
    "        \n",
    "        for i, n in enumerate(nums):\n",
    "            for j in range(i + 1, N + 1):\n",
    "                if j == N: break\n",
    "                if (j - i) & 1 and nums[j] == nums[i] + 1:\n",
    "                    continue\n",
    "                elif (j - i + 1) & 1 and nums[j] == nums[i]:\n",
    "                    continue\n",
    "                break\n",
    "            if (j - i) > 1:\n",
    "                res = max(res, j - i)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        r = -1\n",
    "        n = len(nums)\n",
    "        i = 0\n",
    "        while i<n-1:\n",
    "            if nums[i+1]==nums[i]+1:\n",
    "                j = i+2\n",
    "                while j<n and nums[j]==nums[j-2]:\n",
    "                    j+=1\n",
    "                r = max(r, j-i)\n",
    "            i += 1\n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        n = len(nums)\n",
    "        i = 0\n",
    "        while i < n-1:\n",
    "            if nums[i+1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] == nums[start] + (i-start) % 2:\n",
    "                i += 1\n",
    "            res = max(res, i-start)\n",
    "            i -= 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans, i, n = 1, 0, len(nums)\n",
    "        while i < n:\n",
    "            if i and nums[i] - nums[i - 1] == 1:\n",
    "                i -= 1\n",
    "            start, k = i, 1\n",
    "            i += 1\n",
    "            while (i < n and nums[i] - nums[i - 1] == k):\n",
    "                k = -k\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "        return ans if ans != 1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        for i in range(len(nums)-1,0,-1):\n",
    "            nums[i]-=nums[i-1]\n",
    "        nums[0]=-1\n",
    "        i,j=0,0\n",
    "        w=-1\n",
    "        while i<len(nums):\n",
    "            if nums[i]==1:\n",
    "                j=i+1\n",
    "                while j<len(nums):\n",
    "                    if nums[j]==nums[j-1]*(-1):\n",
    "                        j+=1\n",
    "                    else:\n",
    "                        break\n",
    "                w=max(w,j-i+1)\n",
    "                i=j\n",
    "            else:\n",
    "                i+=1\n",
    "        return w\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        res = []\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            res.append(nums[i+1]-nums[i])\n",
    "        ans = 0\n",
    "        for i in range(n-1):\n",
    "            if res[i] == 1:\n",
    "                cnt = 1\n",
    "                pos = 1\n",
    "                for j in range(i+1,n-1):\n",
    "                    if res[j] == pos*(-1):\n",
    "                        cnt += 1\n",
    "                        pos = (-1)*pos\n",
    "                    else:\n",
    "                        break\n",
    "                ans = max(ans,cnt)\n",
    "            else:\n",
    "                continue\n",
    "        return ans+1 if ans else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans=-1\n",
    "        i,n=0,len(nums)\n",
    "        while i<n-1:\n",
    "            if nums[i+1]-nums[i]!=1:\n",
    "                i+=1\n",
    "                continue\n",
    "            i0=i\n",
    "            i+=1\n",
    "            while i<n and nums[i]==nums[i0]+(i-i0)%2:\n",
    "                i+=1\n",
    "            ans=max(ans,i-i0)\n",
    "            i-=1\n",
    "        return ans            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        addons = [-1, 1]\n",
    "        for i in range(n):\n",
    "            crt_len = 1\n",
    "            j = i\n",
    "            while j + 1 < n and nums[j + 1] == nums[j] + addons[crt_len % 2]:\n",
    "                crt_len += 1\n",
    "                j += 1\n",
    "            ans = max(ans, crt_len)\n",
    "        return ans if ans > 1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        # 分组循环\n",
    "        # 外层循环，枚举子数组的起点\n",
    "        # 内层循环，扩展子数组的右端点\n",
    "        ans = -1\n",
    "        i,n=0,len(nums)\n",
    "        while i<n-1:\n",
    "            if nums[i+1] - nums[i] != 1:\n",
    "                i+=1\n",
    "                continue\n",
    "            i0=i    # 记录起点\n",
    "            i += 1  # i 的含义变成了子数组的右端点\n",
    "            while i<n and nums[i] == nums[i0+(i-i0)%2]: \n",
    "                i += 1\n",
    "            ans = max(ans,i-i0) # [i0,i)\n",
    "            i -= 1 # 对于本题，需要回退\n",
    "        return ans\n",
    "\n",
    "# 2,3,4,3,4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        i, n = 0, len(nums)\n",
    "        while i < n - 1:\n",
    "            if nums[i + 1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            i0 = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] == nums[i0] + (i - i0) % 2:\n",
    "                i += 1\n",
    "            ans = max(ans, i - i0)\n",
    "            i -= 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans,i=-1,0\n",
    "        size=len(nums)\n",
    "        while i<size-1:\n",
    "            if nums[i+1]-nums[i]!=1:\n",
    "                i+=1\n",
    "                continue\n",
    "            temp=i\n",
    "            i+=1\n",
    "            while i<size and nums[temp]+(i-temp)%2==nums[i]:\n",
    "                i+=1\n",
    "            ans=max(ans,i-temp)\n",
    "            i-=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        left = 0\n",
    "        n = len(nums)\n",
    "        res = -1\n",
    "        while left < n - 1:\n",
    "            if nums[left] + 1 != nums[left + 1]:\n",
    "                left += 1\n",
    "                continue\n",
    "            i0 = left \n",
    "            left += 1\n",
    "            while left < n  and nums[left] == nums[i0] + (left - i0) % 2:\n",
    "                left += 1\n",
    "            res = max(res, left - i0)\n",
    "            left -= 1\n",
    "            \n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        maxa=-1\n",
    "        last=nums[0]\n",
    "        n=1\n",
    "        for i in nums:\n",
    "            if i-last != (-1)**(n+1):\n",
    "                n=1\n",
    "            if i-last == (-1)**(n+1):\n",
    "                n+=1\n",
    "                maxa=max(maxa,n)\n",
    "            last=i\n",
    "        return maxa\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        i,n,ret=0,len(nums),-1\n",
    "        while i<n:\n",
    "            left=i\n",
    "            flag=1\n",
    "            while i<n-1 and nums[i+1]-nums[i]==flag:\n",
    "                flag=-flag\n",
    "                i+=1\n",
    "            if i==left:\n",
    "                i+=1\n",
    "            else:\n",
    "                ret=max(ret,i-left+1)\n",
    "        return ret\n",
    "            \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        i, n = 0, len(nums)\n",
    "        while i < n - 1:\n",
    "            if nums[i + 1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            i0 = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] == nums[i0] + (i - i0) % 2:\n",
    "                i += 1\n",
    "            ans = max(ans, i - i0)\n",
    "            i -= 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)  # 获取列表的长度\n",
    "        dif = 1  # 差异值，用于判断交替子数组的连续性\n",
    "        maxLength, curLength = -1, 1  # 最大长度和当前长度，初始化为-1和1\n",
    "        i = 1  # 循环变量，从第二个元素开始遍历\n",
    "        while i < n:\n",
    "            if nums[i]-nums[i-1] == dif:  # 如果当前元素与前一个元素的差异值等于预期的差异值\n",
    "                curLength += 1  # 当前长度加1\n",
    "                dif *= -1  # 差异值取反，用于判断下一个元素的连续性\n",
    "                i += 1  # 继续遍历下一个元素\n",
    "            else:  # 如果当前元素与前一个元素的差异值不等于预期的差异值\n",
    "                maxLength = max(maxLength, curLength)  # 将当前长度与最大长度进行比较，取较大值赋给最大长度\n",
    "                if nums[i]-nums[i-1] == 1:  # 如果当前元素与前一个元素的差异值为1，说明连续性被打破\n",
    "                    curLength = 2  # 当前长度重置为2\n",
    "                    dif = -1  # 差异值设为-1，用于判断下一个元素的连续性\n",
    "                else:  # 如果当前元素与前一个元素的差异值不为1\n",
    "                    dif = 1  # 差异值设为1，用于判断下一个元素的连续性\n",
    "                    curLength = 1  # 当前长度重置为1\n",
    "                i += 1  # 继续遍历下一个元素\n",
    "\n",
    "        res = max(maxLength, curLength)  # 将最大长度与当前长度中的较大值赋给结果变量\n",
    "        return res if res > 1 else -1  # 如果结果变量大于1，则返回结果变量，否则返回-1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        bStart = False\n",
    "        sign = 1\n",
    "        ll = -1\n",
    "        ml = -1\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] - nums[i - 1] == 1 and not bStart:\n",
    "                bStart = True\n",
    "                ll = 2\n",
    "                ml = max(ml, ll)\n",
    "                sign *= -1\n",
    "            if bStart and nums[i] - nums[i - 1] == sign:\n",
    "                ll += 1\n",
    "                ml = max(ml, ll)\n",
    "                sign *= -1\n",
    "            else:\n",
    "                if nums[i] - nums[i - 1] == 1:\n",
    "                    ll = 2\n",
    "                    bStart = True\n",
    "                    sign = -1\n",
    "                else:\n",
    "                    bStart = False\n",
    "                    ll = -1\n",
    "                    sign = 1\n",
    "        return ml\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        group = [-1]\n",
    "        pre = 100000\n",
    "        n = len(nums)\n",
    "        i = 0\n",
    "        while i<n:\n",
    "            now = [nums[i]]\n",
    "            i += 1\n",
    "            j = i\n",
    "            while j < n:\n",
    "                if len(now)==1:\n",
    "                    if nums[j]-now[-1]==1:\n",
    "                        now.append(nums[j])\n",
    "                    else:\n",
    "                        break\n",
    "                else:\n",
    "                    if now[-1] - now[-2]==1 and nums[j] - now[-1] == -1:\n",
    "                            now.append(nums[j])\n",
    "                    elif now[-1] - now[-2]==-1 and nums[j] - now[-1] == 1:\n",
    "                            now.append(nums[j])\n",
    "                    else:\n",
    "                        break\n",
    "                j+=1\n",
    "            \n",
    "            if len(now)>1:\n",
    "                # print(now)\n",
    "                i = j-1\n",
    "                group.append(len(now))\n",
    "        return max(group)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        i = 0\n",
    "        ans = -1\n",
    "\n",
    "        while i < n:\n",
    "            st = i\n",
    "            m = 0\n",
    "            i += 1\n",
    "            while i < n and nums[i] - nums[i-1] == (-1) ** m:\n",
    "                i += 1\n",
    "                m += 1\n",
    "            \n",
    "            ans = max(ans, i - st if i - st > 1 else -1)\n",
    "            i = st + 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans,i = 0,0\n",
    "        n = len(nums)\n",
    "        while i<n:\n",
    "            if i==n-1 or  nums[i+1] != nums[i] + 1:\n",
    "                i+=1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 2\n",
    "            while i<n and nums[i]==nums[(i-start)%2+start]:\n",
    "                i+=1\n",
    "            ans = max(ans,i-start)\n",
    "            i-=1\n",
    "        if not ans:return -1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        i = ini = 0\n",
    "        ans = -1\n",
    "        n = len(nums)\n",
    "        while i < n:\n",
    "            if i + 1 < n and nums[i]+1 == nums[i+1]:\n",
    "                ini = i\n",
    "                ans = max(ans, 2)\n",
    "                i += 1\n",
    "                while i + 1 < n and nums[i-1] == nums[i+1]:\n",
    "                    ans = max(ans, i - ini + 2)\n",
    "                    i += 1\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        r,i,j=-1,0,1\n",
    "        flag=1\n",
    "        t=1\n",
    "        while j<len(nums):\n",
    "            if nums[i]+flag==nums[j]:\n",
    "                t=t+1\n",
    "                j=j+1\n",
    "                flag=0 if flag==1 else 1\n",
    "            else:\n",
    "                r=max(r,t) if t>1 else r\n",
    "                t=1\n",
    "                flag=1\n",
    "                i=i+1\n",
    "                j=i+1\n",
    "        r=max(r,t) if t>1 else r\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        def judge(nums):\n",
    "            if nums[1] != nums[0]+1:\n",
    "                return False\n",
    "            l = len(nums)\n",
    "            for i in range(0,l,2):\n",
    "                if nums[i] != nums[0]:\n",
    "                    return False\n",
    "            for i in range(1,l,2):\n",
    "                if nums[i] != nums[1]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        l = len(nums)\n",
    "        for lenth in range(l,1,-1):\n",
    "            for idx in range(l-lenth+1):\n",
    "                if judge(nums[idx:idx+lenth]):\n",
    "                    return lenth \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        n = len(nums)\n",
    "        delta = 1\n",
    "        while i < n:       \n",
    "            start = i\n",
    "            i += 1\n",
    "            delta = 1\n",
    "            while i < n and nums[i] - nums[i-1] == delta:\n",
    "                i += 1\n",
    "                delta = - delta\n",
    "            ans = max(ans, i - start)\n",
    "            if i < n and nums[i] - nums[i-1] == 1:\n",
    "                i -= 1\n",
    "        \n",
    "        return ans if ans != 1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        result=0\n",
    "        curlen=0\n",
    "        for i,num in enumerate(nums):\n",
    "            if not curlen:\n",
    "                curlen=1\n",
    "            else:\n",
    "                if ((curlen&1)!=0 and num-nums[i-1]==1) or ((curlen&1)==0 and num-nums[i-1]==-1):\n",
    "                    curlen+=1\n",
    "                else:\n",
    "                    result=max(result,curlen)\n",
    "                    curlen=1 if num-nums[i-1]!=1 else 2\n",
    "        result=max(result,curlen)\n",
    "        return result if result!=1 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, a: List[int]) -> int:\n",
    "        n=len(a)\n",
    "        ans=-1\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                c=1\n",
    "                f=True\n",
    "                for k in range(i+1,j+1):\n",
    "                    if a[k]-a[k-1]!=c:\n",
    "                        f=False\n",
    "                        break\n",
    "                    c=-c\n",
    "                if f:\n",
    "                    ans=max(ans,j-i+1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        i, n = 0, len(nums)\n",
    "        while i < n - 1:\n",
    "            if nums[i + 1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            i0 = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] == nums[i0] + (i - i0) % 2:\n",
    "                i += 1\n",
    "            ans = max(ans, i - i0)\n",
    "            i -= 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n,t=0,-1\n",
    "        l=len(nums)\n",
    "        def check(x):\n",
    "            global n\n",
    "            n=1\n",
    "            i=1\n",
    "            while x<len(nums)-1:\n",
    "                if nums[x+1]==nums[x]+i:\n",
    "                    n+=1\n",
    "                    x+=1\n",
    "                    i*=(-1)\n",
    "                    continue\n",
    "                else:\n",
    "                    break\n",
    "            if n==1:\n",
    "                return -1\n",
    "            else:\n",
    "                return n\n",
    "        for i in range(l-1):\n",
    "            t=max(t,check(i))\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        i, ans = 1, -1\n",
    "        while i < n:\n",
    "            c = 0\n",
    "            while i < n and nums[i] - nums[i - 1] == (-1) ** c:\n",
    "                c += 1\n",
    "                i += 1\n",
    "            if c > 0:\n",
    "                ans = max(ans, c + 1)\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        f=[[1]*2 for _ in range(n)]\n",
    "        res=-1\n",
    "        for i in range(1,n):\n",
    "            if nums[i-1]-nums[i]==1 and f[i-1][1]>1:\n",
    "                f[i][0]=f[i-1][1]+1\n",
    "            elif nums[i]-nums[i-1]==1 :\n",
    "                f[i][1]=f[i-1][0]+1\n",
    "            if max(f[i][0],f[i][1])<2:\n",
    "                continue\n",
    "            res=max(res,f[i][0],f[i][1])\n",
    "        return res\n",
    "                  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        i,a,c,t=1,1,0,0\n",
    "        while i <=len(nums)-1:\n",
    "            if a%2==1 and nums[i]-nums[i-1]==1:\n",
    "                a+=1\n",
    "                i+=1\n",
    "            elif a%2==0 and nums[i]-nums[i-1]==-1:\n",
    "                a+=1\n",
    "                i+=1\n",
    "            elif a==1 and t==2:\n",
    "                i+=1\n",
    "                t=0    \n",
    "            else:\n",
    "                a=1\n",
    "                t+=1\n",
    "            c=max(c,a)\n",
    "        if c==1:\n",
    "            return -1 \n",
    "        else:\n",
    "            return c       \n",
    "\n",
    "                \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        i = 1\n",
    "        ans = -1\n",
    "        while i < n:\n",
    "            if nums[i] - nums[i - 1] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i - 1\n",
    "            i += 1\n",
    "            while i < n and nums[i] - nums[i-1] == -(nums[i-1] - nums[i-2]):\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        dp = [0] * len(nums)\n",
    "        if nums[1] - nums[0] == 1:\n",
    "            dp[1] = 2\n",
    "        for i in range(2, len(nums)):\n",
    "            if dp[i - 1] >= 2:\n",
    "                if nums[i - 1] - nums[i - 2] == 1 and nums[i] - nums[i - 1] == -1:\n",
    "                    dp[i] = dp[i - 1] + 1\n",
    "                    continue\n",
    "                elif nums[i - 1] - nums[i - 2] == -1 and nums[i] - nums[i - 1] == 1:\n",
    "                    dp[i] = dp[i - 1] + 1\n",
    "                    continue                 \n",
    "            if nums[i] - nums[i - 1] == 1:\n",
    "                dp[i] = 2\n",
    "\n",
    "        ans = max(dp)\n",
    "        return ans if ans != 0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        ans=0\n",
    "        gap=1\n",
    "        cur_len=0\n",
    "        cur=0\n",
    "        stop=0\n",
    "        while cur<n-1:\n",
    "            if nums[cur+1]==nums[cur]+gap:\n",
    "                cur_len=max(1,cur_len)\n",
    "                cur_len+=1\n",
    "                ans=max(ans,cur_len)\n",
    "                gap=-gap\n",
    "                cur+=1\n",
    "                stop=0\n",
    "            elif stop==0:    # 从新开始\n",
    "                gap=1\n",
    "                cur_len=0\n",
    "                stop=1\n",
    "            else:   # 从新开始也不行\n",
    "                cur+=1\n",
    "        if not ans:\n",
    "            return -1\n",
    "        return ans\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans,l,judge = -1,0,0\n",
    "        lenth = len(nums)\n",
    "        for i in range(1,lenth):\n",
    "            if judge == 1:\n",
    "                if (i - l) % 2 == 0 and nums[i] != nums[l]:\n",
    "                    ans = max(ans,i-l)\n",
    "                    judge = 0\n",
    "                elif (i - l) % 2 == 1 and nums[i] != nums[l+1]:\n",
    "                    ans = max(ans,i-l)\n",
    "                    judge = 0\n",
    "            if judge == 0 and nums[i] - nums[i-1] == 1:\n",
    "                l,judge = i-1,1\n",
    "        if judge == 1:\n",
    "            ans = max(ans,len(nums)-l)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\r\n",
    "        n = len(nums)\r\n",
    "        ans = -1\r\n",
    "        seen = set() # 用來記錄可以做為起點的下標\r\n",
    "        for i in range(n):\r\n",
    "            if i in seen:\r\n",
    "                continue\r\n",
    "            seen.add(i)\r\n",
    "            cur = 1\r\n",
    "            sign = 1\r\n",
    "            for j in range(i+1, n):\r\n",
    "                if nums[j] - nums[j-1] == sign:\r\n",
    "                    cur += 1\r\n",
    "                    sign *= -1\r\n",
    "                    if sign == 1:\r\n",
    "                        seen.add(j)\r\n",
    "                else:\r\n",
    "                    break\r\n",
    "            if cur > 1:\r\n",
    "                ans = max(ans, cur)\r\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        sam = 0\n",
    "        m = 1\n",
    "        b = -1\n",
    "        i = 0\n",
    "        while i<n:\n",
    "            if (sam ==0):\n",
    "                sam+=1\n",
    "            else:\n",
    "                if (nums[i])==(nums[i-1]+m):\n",
    "                    sam+=1\n",
    "                    m =m *(-1)\n",
    "                    b=max(b,sam)\n",
    "                else:\n",
    "                    if(sam!=1):\n",
    "                        i-=2\n",
    "                    else:\n",
    "                        i-=1\n",
    "                    m = 1\n",
    "                    sam = 0\n",
    "            i+=1\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        i, n = 0, len(nums)\n",
    "        while i < n - 1:\n",
    "            if nums[i + 1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            i0 = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] == nums[i0] + (i - i0) % 2:\n",
    "                i += 1\n",
    "            ans = max(ans, i - i0)\n",
    "            i -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans, i, n = 1, 0, len(nums)\n",
    "        while i < n:\n",
    "            if i and nums[i] - nums[i - 1] == 1:\n",
    "                i -= 1\n",
    "            start, k = i, 1\n",
    "            i += 1\n",
    "            while (i < n and nums[i] - nums[i - 1] == k):\n",
    "                k = -k\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "        return ans if ans != 1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\r\n",
    "        ans = -1\r\n",
    "        fast = 1\r\n",
    "        slow = 0\r\n",
    "        while fast < len(nums):\r\n",
    "            if nums[fast] - nums[slow] == 1 and (fast - slow) % 2 == 1:\r\n",
    "                fast += 1\r\n",
    "            elif  nums[fast] - nums[slow] == 0 and (fast - slow) % 2 == 0:\r\n",
    "                fast += 1\r\n",
    "            else:\r\n",
    "                ans = max(ans, fast-slow)\r\n",
    "                slow += 1\r\n",
    "                fast = slow + 1\r\n",
    "        ans = max(ans, fast-slow)\r\n",
    "        if ans == 1:\r\n",
    "            return -1\r\n",
    "        else:\r\n",
    "            return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        i = 0\n",
    "        ans = -1\n",
    "        while i < n-1:\n",
    "            if nums[i+1]-nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            i0 = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] == nums[i0]+(i-i0)%2:\n",
    "                i += 1\n",
    "            ans = max(ans,i-i0)\n",
    "            i -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        r,i,j=-1,0,1\n",
    "        flag=1\n",
    "        t=1\n",
    "        while j<len(nums):\n",
    "            if nums[i]+flag==nums[j]:\n",
    "                t=t+1\n",
    "                j=j+1\n",
    "                flag=0 if flag==1 else 1\n",
    "            else:\n",
    "                r=max(r,t) if t>1 else r\n",
    "                t=1\n",
    "                flag=1\n",
    "                i=i+1\n",
    "                j=i+1\n",
    "        r=max(r,t) if t>1 else r\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n,t=0,-1\n",
    "        l=len(nums)\n",
    "        def check(x):\n",
    "            global n\n",
    "            n=1\n",
    "            i=1\n",
    "            while x<len(nums)-1:\n",
    "                if nums[x+1]==nums[x]+i:\n",
    "                    n+=1\n",
    "                    x+=1\n",
    "                    i*=(-1)\n",
    "                    continue\n",
    "                else:\n",
    "                    break\n",
    "            if n==1:\n",
    "                return -1\n",
    "            else:\n",
    "                return n\n",
    "        for i in range(l-1):\n",
    "            t=max(t,check(i))\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        cnt = 1\n",
    "        res = -1\n",
    "        need = 1\n",
    "        for i in range(1, n):\n",
    "            if nums[i] - nums[i - 1] == need:\n",
    "                cnt += 1\n",
    "                need *= -1\n",
    "                if cnt > 1:\n",
    "                    res = max(res, cnt)\n",
    "            elif nums[i] - nums[i - 1] == 1:\n",
    "                cnt = 2\n",
    "                need = -1\n",
    "            else:\n",
    "                need = 1\n",
    "                cnt = 1\n",
    "            # print(cnt, need)\n",
    "        return res\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        start = -1\n",
    "        n = len(nums)\n",
    "        delta = 1\n",
    "        while i < n:\n",
    "            if start == i - 1:\n",
    "                i += 1\n",
    "            start = i - 1\n",
    "            delta = 1\n",
    "            while i < n and nums[i] - nums[i-1] == delta:\n",
    "                i += 1\n",
    "                delta = - delta\n",
    "            ans = max(ans, i - start)\n",
    "        \n",
    "        return ans if ans != 1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n, res, l = len(nums), 0, 0\n",
    "        while l < n - 1:\n",
    "            if nums[l+1] - nums[l] != 1:\n",
    "                l += 1\n",
    "                continue\n",
    "            r = l + 1\n",
    "            while r < n and nums[r] - nums[r-1] == (-1) ** (r - l + 1):\n",
    "                r += 1\n",
    "            res = max(res, r - l)\n",
    "            l = r - 1\n",
    "        return res if res > 1 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        i=0\n",
    "        ans=-1\n",
    "        n=len(nums)\n",
    "        while i<len(nums)-1:\n",
    "            if nums[i+1]-nums[i]!=1:\n",
    "                i+=1\n",
    "            else:\n",
    "                i0=i \n",
    "                i+=1\n",
    "                while i<n and nums[i]==nums[i0]+(i-i0)%2:\n",
    "                    i+=1\n",
    "                ans=max(ans,i-i0)\n",
    "                i-=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        res=-1\n",
    "        i=0\n",
    "        while i<n-1:\n",
    "            if nums[i+1]-nums[i]!=1:\n",
    "                i+=1\n",
    "                continue\n",
    "            start=i\n",
    "            i+=1\n",
    "            while i<n and nums[i]-nums[start]==(i-start)%2:\n",
    "                i+=1\n",
    "            res=max(res,i-start)\n",
    "            i-=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans = -1\n",
    "        i = 0\n",
    "        while i < n-1:\n",
    "            if nums[i+1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i \n",
    "            i += 1\n",
    "            while i < n and nums[i] - nums[start] == (i - start) % 2:\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "            i -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n, res, l = len(nums), 0, 0\n",
    "        while l < n - 1:\n",
    "            if nums[l+1] - nums[l] != 1:\n",
    "                l += 1\n",
    "                continue\n",
    "            r = l + 1\n",
    "            while r < n and nums[r] == nums[l] + (r - l) % 2:\n",
    "                r += 1\n",
    "            res = max(res, r - l)\n",
    "            l = r - 1\n",
    "        return res if res > 1 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        i = 0\n",
    "        n = len(nums)\n",
    "\n",
    "        while i < n - 1:\n",
    "            if nums[i + 1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "\n",
    "            k = i + 2\n",
    "\n",
    "            while k < n and nums[k] == nums[k - 2]:\n",
    "                k += 1\n",
    "\n",
    "            res = max(res, k - i)\n",
    "            i = k - 1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        i,n = 0,len(nums)\n",
    "        while i<n-1:\n",
    "            if nums[i+1]-nums[i]!=1:\n",
    "                i += 1\n",
    "                continue\n",
    "            i0 = i\n",
    "            i += 1\n",
    "            while i<n and nums[i]==nums[i0]+(i-i0)%2:\n",
    "                i += 1\n",
    "            ans = max(ans,i-i0)\n",
    "            i -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n - 1:\n",
    "            if nums[i + 1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "\n",
    "            t = 1\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] - nums[i - 1] == t:\n",
    "                t = -t\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "            i -= 1\n",
    "        return ans if ans != 0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        i = 0 \n",
    "        n = len(nums)\n",
    "        ans = 0\n",
    "        while i<n:\n",
    "            st = i\n",
    "            i += 1\n",
    "            flag = 1\n",
    "            while i<n and nums[i]==nums[i-1]+flag:\n",
    "                flag = -flag\n",
    "                i += 1\n",
    "            ans = max(ans,i-st)\n",
    "            if i!=st+1:\n",
    "                i-=1\n",
    "        if ans==1:\n",
    "            return -1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "\n",
    "        i, n = 0, len(nums)\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i += 1\n",
    "            # 条件2 第一项必须是+1\n",
    "            if not (i < n and (nums[i] - nums[i - 1]) == 1):\n",
    "                continue\n",
    "            \n",
    "            # 条件3\n",
    "            diff = 1\n",
    "            while i < n and (nums[i] - nums[i - 1]) == diff:\n",
    "                i += 1\n",
    "                diff *= -1\n",
    "\n",
    "            end = i - 1\n",
    "            length = end - start + 1\n",
    "            # 回溯到check=false相减的前一个位置\n",
    "            i += -1\n",
    "\n",
    "            # 条件1 根据上面的限制，天然满足长度>=2\n",
    "            # if length > 1:\n",
    "            ans = max(ans, length)\n",
    "\n",
    "        return ans\n",
    "\n",
    "'''\n",
    "# 灵神关于`分组循环`的简单介绍与题单\n",
    "\n",
    "## 分组循环\n",
    "\n",
    "无需特判 `nums` 是否为空，也无需在循环结束后，再补上处理最后一段区间的逻辑。\n",
    "\n",
    "以我的经验，这种写法是所有写法中最不容易出 bug 的，推荐大家记住。\n",
    "\n",
    "适用场景：按照题目要求，数组会被分割成若干段，且每一段的判断/处理逻辑是一样的。\n",
    "\n",
    "注：虽然代码写的是一个二重循环，但 `i += 1` 这句话至多执行 n 次，所以总的时间复杂度仍然是 `O(n)` 的。\n",
    "\n",
    "一般来说，分组循环的模板如下（根据题目调整）：\n",
    "\n",
    "```python\n",
    "i, n = 0, len(nums)\n",
    "while i < n:\n",
    "    start = i\n",
    "    while i < n and ...:\n",
    "        i += 1\n",
    "    # 从 start 到 i-1 是一段\n",
    "    # 下一段从 i 开始，无需 i+=1\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "### 练习\n",
    "\n",
    "- [1446. 连续字符](https://leetcode.cn/problems/consecutive-characters/)\n",
    "- [1869. 哪种连续子字符串更长](https://leetcode.cn/problems/longer-contiguous-segments-of-ones-than-zeros/)\n",
    "- [1957. 删除字符使字符串变好](https://leetcode.cn/problems/delete-characters-to-make-fancy-string/)\n",
    "- [2038. 如果相邻两个颜色均相同则删除当前颜色](https://leetcode.cn/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/)\n",
    "- [1759. 统计同质子字符串的数目](https://leetcode.cn/problems/count-number-of-homogenous-substrings/)\n",
    "- [2110. 股票平滑下跌阶段的数目](https://leetcode.cn/problems/number-of-smooth-descent-periods-of-a-stock/)\n",
    "- [1578. 使绳子变成彩色的最短时间](https://leetcode.cn/problems/minimum-time-to-make-rope-colorful/)\n",
    "- [1839. 所有元音按顺序排布的最长子字符串](https://leetcode.cn/problems/longest-substring-of-all-vowels-in-order/)\n",
    "- [2760. 最长奇偶子数组](https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/)\n",
    "- [2765. 最长交替子序列](https://leetcode.cn/problems/longest-alternating-subarray/)\n",
    "- [228. 汇总区间](https://leetcode.cn/problems/summary-ranges/)\n",
    "- [56. 合并区间](https://leetcode.cn/problems/merge-intervals/)\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        dp = [1]*n\n",
    "        for i in range(1,n):\n",
    "            if nums[i] - nums[i-1] ==1:\n",
    "                if dp[i-1]%2==0:\n",
    "                    dp[i] =2\n",
    "                else:\n",
    "                    dp[i] = dp[i-1] + 1\n",
    "            if nums[i] - nums[i-1] ==-1 and dp[i-1]!=1:\n",
    "                if dp[i-1]%2==1:\n",
    "                    dp[i]=1\n",
    "                else:\n",
    "                    dp[i] = dp[i-1] + 1\n",
    "        if max(dp)==1:\n",
    "            return -1\n",
    "        return max(dp)\n",
    "        # i =1\n",
    "        # ans = []\n",
    "        # while i <len(nums):\n",
    "        #     if nums[i]-nums[i-1]:\n",
    "        #         cnt = str(nums).count(str(nums[i-1])+', '+str(nums[i]))\n",
    "        #         if i-1+cnt*2+1<len(nums): \n",
    "        #             if nums[i+cnt*2] ==nums[i]:\n",
    "        #                 ans.append(cnt*2+1)\n",
    "        #                 i= i+cnt*2+1\n",
    "        #             else:\n",
    "        #                 ans.append(cnt*2)\n",
    "        #                 i = i+cnt*2\n",
    "        #         else:\n",
    "        #             break\n",
    "        # return max(ans)+1 if len(ans)>0 and max(ans)>0 else -1\n",
    "        # #return ans\n",
    "        \n",
    "        \n",
    "        # # cnt =1\n",
    "        # # i = 1\n",
    "        # # ans =[]\n",
    "        # # flag = True\n",
    "        # # while i <len(nums):\n",
    "        # #     if flag:\n",
    "        # #         if nums[i]-nums[i-1]==1:\n",
    "        # #             cnt +=1\n",
    "                    \n",
    "        # #         else:\n",
    "        # #             ans.append(cnt)\n",
    "        # #             cnt =1\n",
    "        # #         flag = not flag\n",
    "                \n",
    "        # #     else:\n",
    "        # #         if nums[i]-nums[i-1]==-1:\n",
    "        # #             cnt +=1\n",
    "        # #         else:\n",
    "        # #             ans.append(cnt)\n",
    "        # #             cnt =1\n",
    "        # #         flag = not flag\n",
    "        # #     i+=1\n",
    "        # # return ans\n",
    "        # # return max(ans) if len(ans)>0 and max(ans) >1 else -1\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # # # cnt =0\n",
    "        # # # ans =-1\n",
    "        # # # i = 1\n",
    "        # # # while i <len(nums):\n",
    "        # # #     if nums[i] <nums[i-1]:\n",
    "        # # #         cnt +=1\n",
    "        # # #         j = i+1\n",
    "        # # #         while j <len(nums):\n",
    "        # # #             if i <len(nums) and nums[j]<nums[i] :\n",
    "        # # #                 cnt +=1\n",
    "        # # #                 i=j+1\n",
    "        # # #             else:\n",
    "                 \n",
    "        # # #                i +=1\n",
    "        # # #         ans = max(ans,cnt)\n",
    "        # # #         cnt =0           \n",
    "        # # #     else:\n",
    "        # # #         i+=1\n",
    "                \n",
    "                \n",
    "        # # # return ans if ans != 0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        \n",
    "        max1 = 0\n",
    "        for i in range(len(nums)-1):\n",
    "            count = 1\n",
    "            j = i+1\n",
    "            index = i\n",
    "            k = 1\n",
    "            while k*(nums[j]-nums[index])==1:\n",
    "                if j==len(nums)-1:\n",
    "                    count += 1\n",
    "                    break\n",
    "                index = j\n",
    "                j += 1\n",
    "                k = -1*k\n",
    "                count += 1\n",
    "            if(max1<=count):\n",
    "                max1 = count\n",
    "        if max1==1:\n",
    "            return -1\n",
    "        return max1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "\r\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\r\n",
    "\r\n",
    "        res = -1\r\n",
    "\r\n",
    "        a = 0\r\n",
    "        while True:\r\n",
    "            m = 2\r\n",
    "            b = a + m\r\n",
    "            while b <= len(nums) and nums[b - 1] - nums[b - 2] == (-1)**m:\r\n",
    "                m += 1\r\n",
    "                b = a + m\r\n",
    "            m -= 1\r\n",
    "            b = a + m\r\n",
    "\r\n",
    "            if m >= 2:\r\n",
    "                res = max(res, m)\r\n",
    "                a = b - 1\r\n",
    "            else:\r\n",
    "                a += 1\r\n",
    "            if a >= len(nums) - 1: break\r\n",
    "\r\n",
    "        return res\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        i = 0\n",
    "        ans = 0\n",
    "        while i < n:\n",
    "            l = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] == nums[l] + ((i % 2) ^ (l % 2) == 1):\n",
    "                i += 1\n",
    "            if i - l > 1:\n",
    "                ans = max(ans, i - l)\n",
    "            if i - l > 1 and i < n:\n",
    "                i -= 1\n",
    "        return ans if ans else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def alternatingSubarray(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        i, n = 0, len(nums)\n",
    "        while i < n - 1:\n",
    "            if nums[i + 1] - nums[i] != 1:\n",
    "                i += 1\n",
    "                continue\n",
    "            i0 = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] == nums[i0] + (i - i0) % 2:\n",
    "                i += 1\n",
    "            ans = max(ans, i - i0)\n",
    "            i -= 1\n",
    "        return ans\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
